package ita.class1.group2.supermarket.merchant.controller;

import ita.class1.group2.supermarket.common.annotation.Autowired;
import ita.class1.group2.supermarket.common.annotation.Component;
import ita.class1.group2.supermarket.common.annotation.RequestMapping;
import ita.class1.group2.supermarket.common.auth.SecurityUtils;
import ita.class1.group2.supermarket.common.auth.UsernamePasswordToken;
import ita.class1.group2.supermarket.common.pojo.ResponseMessage;
import ita.class1.group2.supermarket.common.util.API;
import ita.class1.group2.supermarket.common.util.HttpUtil;
import ita.class1.group2.supermarket.common.util.JsonUtil;
import ita.class1.group2.supermarket.common.util.Md5Utils;
import ita.class1.group2.supermarket.merchant.pojo.StoreForm;
import ita.class1.group2.supermarket.merchant.pojo.User;
import java.util.HashMap;
import java.util.Map;

@Component
public class StoreController {

    @Autowired
    private SecurityUtils securityUtils;

    @RequestMapping("/store")
    public StoreForm getStoreForm() throws Exception {
        securityUtils.getSubject().login(new UsernamePasswordToken("burgess", Md5Utils.md5Password("123456")));
        User user = (User) securityUtils.getSubject().getAuthorizingInfo();

        Map<String, String> params = new HashMap<>();
        params.put("USER_ID", user.getId());
        String storeFormJson  = HttpUtil.doPost(API.GET_STORE_FORM, params);

        if (storeFormJson != null && !storeFormJson.isEmpty()) {
            StoreForm storeForm = JsonUtil.json2pojo(JsonUtil.obj2json(JsonUtil.json2pojo(storeFormJson, ResponseMessage.class).getContent()), StoreForm.class);
            return storeForm;
        }

        return null;
    }

}
